package com.itany.service.impl;

import com.itany.entity.Message;
import com.itany.entity.User;
import com.itany.mapper.MessageMapper;
import com.itany.mapper.UserMapper;
import com.itany.service.AreaService;
import com.itany.service.ExamineService;
import com.itany.service.MessageService;
import com.itany.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper mapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name, String phone, String companyid) {
        List<Message> list = mapper.selectByParams(name,phone,companyid);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()) {
            for (Message message : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",message.getId());
                u.put("name",message.getName());
                u.put("phone",message.getPhone());
                u.put("area",message.getArea());
                u.put("serverCompany",message.getServerCompany());
                u.put("createdate",message.getCreatedate());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public String selectMsgById(Integer id) {
        Message message =  mapper.selectByPrimaryKey(id);
        return message.getMsg();
    }
}
